package bigraph.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import bigraph.model.Root;
import bigraph.model.Place;

public class PlaceCreateCommand extends Command {

	private Root root;
	private Place place;
	private Place parent;
	
	public PlaceCreateCommand() {
	     super();
	     root = null;
	     place = null;
	     parent = null;
	}
	public void setPlace(Object s) {
	     if (s instanceof Place)
	            this.place = (Place)s;
	}
	public void setRoot(Object e) {
	     if (e instanceof Root)
	            this.root = (Root)e;
	     if (e instanceof Place)
	            this.parent = (Place)e;
	}
	public void setLayout(Rectangle r) {
	     if (place == null)
	            return;
	     place.setLayout(r);
	}
	@Override
	public boolean canExecute() {
	     if (place == null && (parent == null || root == null))
	            return false;
	     return true;
	}

	@Override
	public void execute() {
		if (root != null)
			root.addChild(place);
		else if (place != null)
			parent.addChild(place);
	}
	@Override
	public boolean canUndo() {
	     if (place == null && (parent == null || root == null))
	           return false;
	     if(root != null)
	     	return root.contains(place);
	     else
	    	 return parent.contains(place);
	}
	@Override
	public void undo() {
		 if (!root.removeChild(place));
		 parent.removeChild(place);
	}

}
